package Practices;

import java.util.Random;

public class VerifiactionCode {
    public static void main(String[] args) {
        //生成验证码：长度为5、小写、大写、1位数字且随机位置；结果为字符串；
        char[] arr = new char[52];
        //A~Z:65-90   a~z:97-122;
        for (int i = 0; i < arr.length; i++) { //arr.length=52,i<52
            if (i <= 25){
                arr[i] = (char)(65+i);// capital
            }else {
                arr[i] = (char)(97+i-26);// lowercase
            }
        }
        //随机取出放进new数组中，长度为5
        while (true) {
            Random random = new Random();
            char[] arr2 = new char[5];
            for (int i = 0; i < arr2.length-1; i++) {
                arr2[i] = arr[random.nextInt(arr.length)];
            }
            //拼接一位数字
            arr2[arr2.length-1] = (char) (random.nextInt(48,58));
            //打乱数组
            for (int i = 0; i < arr2.length; i++) {
                char temp = arr2[i];
                int index =random.nextInt(arr2.length);
                arr2[i] = arr2[index];
                arr2[index] = temp;
            }
            String result = new String(arr2);
            System.out.println(result);
            break;//完美！嘿嘿
        }

    }
}
